package org.acghub.config;

import freemarker.template.Version;
import lombok.Getter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 12:26
 * @Version 1.0
 */
public class TableConfig {

    //************************************************** default value *************************************************
    public static final String DEFAULT_ENCODING = "UTF-8";
    public static final String DEFAULT_PACKAGE_PREFIX = "org.acghub.config.confbean";
    public static final String DEFAULT_OUTPUT_DIR = "D:\\Self\\gitee\\demos\\config\\src\\test\\java";

    //************************************************** default value *************************************************

    private static final Map<ConfigKeyEnum, Object> CONFIGS = new LinkedHashMap<>();

    static {
        put(ConfigKeyEnum.ENCODING, DEFAULT_ENCODING);
        put(ConfigKeyEnum.CODE_GENERATE_PACKAGE_PREFIX, DEFAULT_PACKAGE_PREFIX);
        put(ConfigKeyEnum.CODE_GENERATE_DIR, DEFAULT_OUTPUT_DIR);

    }

    public static String getPackageName(TableMeta meta) {
        return get(ConfigKeyEnum.CODE_GENERATE_PACKAGE_PREFIX) + "." + meta.getModule();
    }

    public static String getClassName(TableMeta meta) {
        return getPackageName(meta) + "." + meta.getTableName();
    }

    public static void put(ConfigKeyEnum key, Object value) {
        CONFIGS.put(key, value);
    }

    public static <T> T get(ConfigKeyEnum key) {
        return (T) CONFIGS.get(key);
    }

    public static <T> T get(ConfigKeyEnum key, T defaultValue) {
        return (T) CONFIGS.getOrDefault(key, defaultValue);
    }

    public static boolean has(ConfigKeyEnum key) {
        return CONFIGS.containsKey(key);
    }

    /**
     * @Desc
     * @Author miyuki4j
     * @Date 2025/7/24/星期四 12:54
     * @Version 1.0
     */
    @Getter
    public enum ConfigKeyEnum {

        ENCODING("encoding", "编码 默认值为UTF-8", false), // 编码

        // read
        INPUT_DIR("input_dir", "数据源目录 绝对路径"), // 数据源目录

        // code generate
        CODE_GENERATE_DIR("code_generate_dir", "代码生成目录 绝对路径"), // 源代码输出目录
        CODE_GENERATE_PACKAGE_PREFIX("code_generate_package_prefix", "代码生成包前缀"), // 包名前缀
        CODE_GENERATOR_TYPE("code_generator_type", "代码生成器类型 freemarker、 java") {
            @Override
            protected CodeGeneratorEnum doParse(String value) throws Exception {
                return CodeGeneratorEnum.of(value);
            }
        }, // 代码生成器类型
        CODE_GENERATE_USE_LOMBOK("code_generate_use_lombok", "代码生成是否启用lombok, 默认值为false", false) {
            @Override
            protected Boolean doParse(String value) throws Exception {
                return Boolean.parseBoolean(value);
            }
        }, // 是否使用lombok

        // export、import
        DATA_EXPORT_DIR("data_export_dir", "数据导出目录 绝对路径"), //数据导出目录
        DATA_EXPORT_TYPE("data_export_type", "数据导出类型 json、xml") {
            @Override
            protected TableDataExchangerEnum doParse(String value) throws Exception {
                return TableDataExchangerEnum.of(value);
            }
        },//数据导出类型
        EXPORT_JSON_PRETTY("export_json_pretty", "导出json是否格式化, 默认值为false", false) {
            @Override
            protected Boolean doParse(String value) throws Exception {
                return Boolean.parseBoolean(value);
            }
        }, // 导出json是否格式化
        DATA_IMPORT_DIR("data_import_dir", "数据导入目录"), //数据导入目录
        DATA_IMPORT_TYPE("data_import_type", "数据导入文件类型 json、xml") {
            @Override
            protected TableDataExchangerEnum doParse(String value) throws Exception {
                return TableDataExchangerEnum.of(value);
            }
        }, //数据导入类型

        // freemarker
        FREEMARKER_VERSION("freemarker_version", "freemarker版本 默认值为2.3.32", false) {
            @Override
            protected Version doParse(String value) throws Exception {
                return new Version(value);
            }
        }, // freemarker版本
        FREEMARKER_TEMPLATE_DIR("freemarker_template_dir", "freemarker模板目录 绝对路径", false), // freemarker模板目录
        FREEMARKER_TEMPLATE_NAME("freemarker_template_name", "freemarker模板文件名称 默认值为ConfigTemplate.ftl", false), // freemarker模板名称


        ;

        private final String value;
        private final String desc;
        private boolean required = true;

        ConfigKeyEnum(String value, String desc, boolean required) {
            this.value = value;
            this.desc = desc;

            this.required = required;
        }

        ConfigKeyEnum(String value, String desc) {
            this(value, desc, true);
        }

        public boolean match(String value) {
            return StringUtils.startsWith(value, "--" + this.value);
        }

        public final <T> T parse(final String value) throws CommandParseException {
            try {
                Objects.requireNonNull(value);
                int i = value.indexOf("=");
                String s = i > 0 ? value.substring(i + 1) : "";
                Object o = doParse(s);
                return (T) Objects.requireNonNull(o, "parse value npe, value=" + value);
            } catch (Exception e) {
                throw new CommandParseException("value" + value, e);
            }
        }

        protected <T> T doParse(String value) throws Exception {
            return (T) value;
        }

        public String usage() {
            return "--" + value + ":" + (required ? "【必填】" : "【选填】") + "\t" + desc;
        }

    }

}
